package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;

import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Permission;
import com.itheima.service.PermissionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author TanLin
 * @date 2019/11/12 20:32
 * @className com.itheima.controller
 * @projectName health_parent
 */
@RequestMapping("/permission")
@RestController
public class PermissionController {

    @Reference
    private PermissionService permissionService;

    /**
     * 分页查询
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPagePermission(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = null;
        try {
            pageResult = permissionService.findPagePermission(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageResult;

    }

    /**
     *添加权限
     * @return
     */
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission){
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.PERMISSION_SUCCESS);
    }

    /**
     * 编辑回显
     * @return
     */
    @GetMapping("/findById")
    public Result findById(Integer id){
        Permission permission =null;
        try {
            permission = permissionService.findById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EXCEPTION);
        }
        return new Result(true,MessageConstant.CHECK_PERMISSION_SUCCESS,permission);
    }

    /**
     * 提交编辑
     * @return
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Permission permission){
        try {
            permissionService.edit(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.UPDATE_PERMISSION_FAIL);
        }
        return new Result(true,MessageConstant.UPDATE_PERMISSION_SUCCESS);

    }

    /**
     * 删除
     * @return
     */
    @GetMapping("/delete")
    public Result delete(Integer id) {
        try {
            permissionService.delete(id);
        } catch (RuntimeException e) {
            return new Result(false, e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_PERMISSION_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_PERMISSION_SUCCESS);
    }
    /**
     * 查询全部
    * @param
    * @return com.itheima.entity.Result
    */
    @RequestMapping("/findAll")
    public Result findAll() {
        List<Permission> list = permissionService.findAll();
        if (list != null && list.size() > 0) {

            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, list);
        }
        return new Result(false, MessageConstant.QUERY_CHECKGROUP_FAIL);
    }}